package de.nordakademie.wpk.share.core.data.treeitem;

import de.nordakademie.wpk.share.core.data.Depot;
import de.nordakademie.wpk.share.core.data.Share;
import de.nordakademie.wpk.share.core.service.ShareService;

/**
 * Generates trees for the ui treeview
 * 
 * @author Christian Baumhof
 * 
 */
public class TreeGenerator {

	/**
	 * Generates a tree containing all shares and the depots they are included
	 * in
	 * 
	 * @return TreeItem
	 */
	public static TreeItem getShareTree() {
		TreeItem root = new TreeItem();
		TreeItem shareTitle = new TreeItem("Aktien");
		root.addChild(shareTitle);

		for (Share share : ShareService.shareContainer.getShares()) {
			shareTitle.addChild(share);
			for (Depot depot : ShareService.depotContainer.getDepots()) {
				if (depot.isShareInDepot(share.getSymbol())) {
					share.addChild(depot);
				}
			}
		}

		return root;
	}

	/**
	 * Generates a tree containing all depots and indices as well as their
	 * shares. Additionally a node containing all shares is included
	 * 
	 * @return TreeItem
	 */
	public static TreeItem getDepotTree() {
		TreeItem root = new TreeItem();
		TreeItem indexTitle = new TreeItem("Indices");
		TreeItem depotTitle = new TreeItem("Depots");
		TreeItem shareTitle = new TreeItem("Aktien");
		root.addChild(indexTitle);
		root.addChild(depotTitle);
		root.addChild(shareTitle);

		for (Depot depot : ShareService.depotContainer.getDepots()) {
			if (depot.getType() == TreeItem.Type.INDEX) {
				indexTitle.addChild(depot);
			} else {
				depotTitle.addChild(depot);
			}
			for (Share share : depot.getShares()) {
				depot.addChild(share);
			}
		}
		for (Share share : ShareService.shareContainer.getShares()) {
			shareTitle.addChild(share);
		}

		return root;
	}
}
